<template>
    <div>
        <p>
            <el-input placeholder="请输入内容" v-model="input" class="input-with-select">
                <el-button slot="append" icon="el-icon-search" @click="search()"></el-button>
            </el-input>
            <el-button type="primary" @click="goAdd()">添加用户</el-button>
        </p>
        <template>
            <el-table :data="users" border style="width: 100%;margin-top: 20px;">
                <el-table-column type="index" width="70">
                </el-table-column>
                <el-table-column prop="username" label="姓名">
                </el-table-column>
                <el-table-column prop="email" label="邮箱">
                </el-table-column>
                <el-table-column prop="mobile" label="电话">
                </el-table-column>
                <el-table-column prop="role_name" label="角色">
                </el-table-column>
                <el-table-column label="状态">
                    <template slot-scope="scope">
                        <el-switch v-model="scope.row.mg_state" @change="goStatus(scope.row)">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button size="mini" type="primary" icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"></el-button>
                        <el-button size="mini" type="danger" icon="el-icon-delete"
                            @click="handleDelete(scope.$index, scope.row)">
                        </el-button>
                        <el-tooltip class="item" effect="dark" content="权限分配" placement="top">
                            <el-button size="mini" type="warning" icon="el-icon-s-tools"
                                @click="role(scope.row)"></el-button>
                        </el-tooltip>
                    </template>
                </el-table-column>
            </el-table>
        </template>

        <!-- 分页 -->
        <div class="block">
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="from.pagenum" :page-sizes="[10, 20, 30, 40]" :page-size="from.pagesize"
                layout="total, sizes, prev, pager, next, jumper" :total="total" v-if="total != 0">
            </el-pagination>
        </div>

        <!-- 添加用户 -->
        <el-dialog :title="`${flag == true ? '修改用户' : '添加用户'}`" :visible.sync="dialogFormVisible">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="ruleForm.username" :disabled="editId == '' ? false : true"></el-input>
                </el-form-item>

                <el-form-item label="密码" prop="password" v-if="editId == ''">
                    <el-input v-model="ruleForm.password"></el-input>
                </el-form-item>

                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="ruleForm.email"></el-input>
                </el-form-item>
                <el-form-item label="手机号" prop="mobile">
                    <el-input type="number" v-model="ruleForm.mobile"></el-input>
                </el-form-item>
                <el-form-item style="text-align:end">
                    <el-button @click="resetForm('ruleForm')">取消</el-button>
                    <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>

                </el-form-item>
            </el-form>
        </el-dialog>

        <!-- 角色分配 -->
        <el-dialog title="分配角色" :visible.sync="dialogFormVisible1">
            <el-form :model="form1">
                <el-form-item label="当前的用户" label-width="120px">

                    <span>{{ form1.username }}</span>


                </el-form-item>
                <el-form-item label="当前的用户" label-width="120px">

                   <span>{{ form1.role_name }}</span> 


                </el-form-item>
                <el-form-item label="分配新角色" label-width="120px">
                    <el-select v-model="form1.region" placeholder="请选择">
                        <el-option :label="item.label" :value="item.value" v-for="(item,index) in option" :key="index" ></el-option>
                        
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible1 = false">取 消</el-button>
                <el-button type="primary" @click="dialogFormVisible1 = false, roles()">确 定</el-button>
            </div>
        </el-dialog>
</div>
</template>
<script>
import { getUsers, getStatus, getAdd, getDelete, getEdit, getSearch, getRole } from "../untils/api"
export default {
    data() {
        return {
            flag: false,//控制添加编辑弹窗
            editId: '',
            users: [],
            role_id: '',
            input: '',
            option:[{label:'管理员',value:321},{label:'员工',value:123}],
            from: {
                pagesize: 10,
                pagenum: 1,
                query: '',
            },
            total: 0,
            ruleForm: {
                username: '',
                password: '',
                email: '',
                mobile: ''
            },
            form1: {

                // roles: '',
                // user: '',
            },
            edid:'',
            dialogFormVisible: false,
            dialogFormVisible1: false,
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    // { min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入你的密码', trigger: 'blur' },
                    { min: 3, max: 16, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                email: [
                    { type: 'email', required: true, message: '请输入你的邮箱', trigger: 'blur' },

                ],
                mobile: [
                    { required: true, message: '请输入你的手机号', trigger: 'blur' },

                ]
            }
        }
    },
    methods: {
        handleSizeChange(val) {
            this.from.pagesize = val;
            // this.from.pagenum = 1;
            // console.log(`每页 ${val} 条`);
            this.page()
            this.pageQie()
        },
        handleCurrentChange(val) {
            this.from.pagenum = val
            this.page()
            this.pageQie()
            // console.log(`当前页: ${val}`);
        },
        page() {
            getUsers(this.from).then(res => {
                // console.log(res);
                this.users = res.data.data.users
                // console.log(11);
                this.total = res.data.data.total

            })
        },
        pageQie() {
            this.users = this.users.slice((this.from.pagenum - 1) * this.from.pagesize, this.from.pagenum * this.from.pagesize)
        },
        async goStatus(row) {//修改状态
            // console.log(row);
            await getStatus(row).then(res => {
                // console.log(res);
            })
        },
        //elment添加
        submitForm(formName) {
            // console.log(this.ruleForm);
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (this.editId) {
                        getEdit(this.ruleForm, this.editId).then(res => {
                            // console.log(res);
                            this.page()
                            this.pageQie()
                        })
                        this.$refs[formName].resetFields();


                    } else {

                        getAdd(this.ruleForm).then(res => {
                            // console.log(res);
                            this.role_id = res.role_id
                            this.page()
                            this.pageQie()
                        })

                        this.$refs[formName].resetFields();

                        // this.$refs[formName].clearValidate()
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
                this.dialogFormVisible = false

            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        goAdd() {
            this.dialogFormVisible = true;
            this.flag = false;
            this.$refs[formName].resetFields();

            this.editId = ''
        },
        //编辑删除
        handleEdit(index, row) {
            this.flag = true
            this.dialogFormVisible = true
            this.ruleForm = row
            this.editId = row.id
            // console.log(index, row);
        },
        handleDelete(index, row) {

            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                getDelete(row).then(res => {
                    this.page()
                    this.pageQie()
                })
                // 为了在删除最后一页的最后一条数据时能成功跳转回最后一页的上一页（pageSize=1时不生效）

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

            // console.log(index, row);
        },
        search() {//搜索

            this.from.query = this.input
            this.page()
            this.input = ''
        },
        role(row) {

            // console.log(row);
            this.dialogFormVisible1 = true;
            //             this.from1.region = row.username
            //             this.form1.roles = row.username
            // ,
            //                 this.form1.user=row.role_name
            this.form1 = row
            this.edid = row.id
            console.log(this.edid);
        },
        roles() {
            // console.log(this.form1.id);
            getRole({ id: this.edid, rid:'511' }).then(res => {
                console.log(res);
                // this.form1.user = res.username;
                // this.form1.roles = res.role_name
            }).catch(err=>{
                console.log(err);
            })
        }

    },
    created() {
        this.page()
        this.pageQie()
    }
}
</script>
<style lang="scss" scoped>
p {
    width: 50%;
    display: flex;

    .el-input {
        margin-right: 20px
    }
}
</style>


